package com.wmeimob.fastboot.jiubei.service;

import com.wmeimob.fastboot.core.exception.CustomException;
import com.wmeimob.fastboot.jiubei.entity.ShopOrders;
import com.wmeimob.fastboot.jiubei.mapper.ShopOrdersMapper;
import com.wmeimob.fastboot.jiubei.enums.ShopOrdersStatusEnum;
import com.wmeimob.fastboot.util.InputValidator;
import com.wmeimob.fastboot.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName ShopOrdersServiceImpl
 * @Description 订单 服务类实现
 * @Author wjt
 * @Date Mon Aug 31 23:57:11 CST 2020
 **/

@Service
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class ShopOrdersServiceImpl implements ShopOrdersService {

    @Resource
    private ShopOrdersMapper shopOrdersMapper;


    @Override
    public List<ShopOrders> list(ShopOrders shopOrders) {
        Example example = new Example(ShopOrders.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(shopOrders.getId())) {
            criteria.andEqualTo("id", shopOrders.getId());
        }
        if (!StringUtils.isEmpty(shopOrders.getOrderNo())) {
            criteria.andLike("orderNo", StringUtils.fullFuzzy(shopOrders.getOrderNo()));
        }
        if (!StringUtils.isEmpty(shopOrders.getUserId())) {
            criteria.andEqualTo("userId", shopOrders.getUserId());
        }
        if (!StringUtils.isEmpty(shopOrders.getUserName())) {
            criteria.andLike("userName", StringUtils.fullFuzzy(shopOrders.getUserName()));
        }
        if (!StringUtils.isEmpty(shopOrders.getUserCode())) {
            criteria.andLike("userCode", StringUtils.fullFuzzy(shopOrders.getUserCode()));
        }
        if (!StringUtils.isEmpty(shopOrders.getOrderType())) {
            criteria.andLike("orderType", StringUtils.fullFuzzy(shopOrders.getOrderType()));
        }
        if (!StringUtils.isEmpty(shopOrders.getMobile())) {
            criteria.andLike("mobile", StringUtils.fullFuzzy(shopOrders.getMobile()));
        }
        if (!StringUtils.isEmpty(shopOrders.getOrderStatus())) {
            criteria.andLike("orderStatus", StringUtils.fullFuzzy(shopOrders.getOrderStatus()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingName())) {
            criteria.andLike("shippingName", StringUtils.fullFuzzy(shopOrders.getShippingName()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingProvince())) {
            criteria.andLike("shippingProvince", StringUtils.fullFuzzy(shopOrders.getShippingProvince()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingCity())) {
            criteria.andLike("shippingCity", StringUtils.fullFuzzy(shopOrders.getShippingCity()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingDistrict())) {
            criteria.andLike("shippingDistrict", StringUtils.fullFuzzy(shopOrders.getShippingDistrict()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingAddress())) {
            criteria.andLike("shippingAddress", StringUtils.fullFuzzy(shopOrders.getShippingAddress()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingMobile())) {
            criteria.andLike("shippingMobile", StringUtils.fullFuzzy(shopOrders.getShippingMobile()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingFee())) {
            criteria.andEqualTo("shippingFee", shopOrders.getShippingFee());
        }
        if (!StringUtils.isEmpty(shopOrders.getTransactionId())) {
            criteria.andLike("transactionId", StringUtils.fullFuzzy(shopOrders.getTransactionId()));
        }
        if (!StringUtils.isEmpty(shopOrders.getOrderAmount())) {
            criteria.andEqualTo("orderAmount", shopOrders.getOrderAmount());
        }
        if (!StringUtils.isEmpty(shopOrders.getPayAmount())) {
            criteria.andEqualTo("payAmount", shopOrders.getPayAmount());
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingDeduction())) {
            criteria.andEqualTo("shippingDeduction", shopOrders.getShippingDeduction());
        }
        if (!StringUtils.isEmpty(shopOrders.getPayAt())) {
            criteria.andEqualTo("payAt", shopOrders.getPayAt());
        }
        if (!StringUtils.isEmpty(shopOrders.getPayType())) {
            criteria.andEqualTo("payType", shopOrders.getPayType());
        }
        if (!StringUtils.isEmpty(shopOrders.getRemark())) {
            criteria.andLike("remark", StringUtils.fullFuzzy(shopOrders.getRemark()));
        }
        if (!StringUtils.isEmpty(shopOrders.getUserComments())) {
            criteria.andLike("userComments", StringUtils.fullFuzzy(shopOrders.getUserComments()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingVendor())) {
            criteria.andLike("shippingVendor", StringUtils.fullFuzzy(shopOrders.getShippingVendor()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingNo())) {
            criteria.andLike("shippingNo", StringUtils.fullFuzzy(shopOrders.getShippingNo()));
        }
        if (!StringUtils.isEmpty(shopOrders.getShippingAt())) {
            criteria.andEqualTo("shippingAt", shopOrders.getShippingAt());
        }
        if (!StringUtils.isEmpty(shopOrders.getReceiptAt())) {
            criteria.andEqualTo("receiptAt", shopOrders.getReceiptAt());
        }
        if (!StringUtils.isEmpty(shopOrders.getCommentAt())) {
            criteria.andEqualTo("commentAt", shopOrders.getCommentAt());
        }
        if (!StringUtils.isEmpty(shopOrders.getClosedAt())) {
            criteria.andEqualTo("closedAt", shopOrders.getClosedAt());
        }
        if (!StringUtils.isEmpty(shopOrders.getClosedReason())) {
            criteria.andLike("closedReason", StringUtils.fullFuzzy(shopOrders.getClosedReason()));
        }
        if (!StringUtils.isEmpty(shopOrders.getSettleCommission())) {
            criteria.andEqualTo("settleCommission", shopOrders.getSettleCommission());
        }
        if (!StringUtils.isEmpty(shopOrders.getComment())) {
            criteria.andEqualTo("comment", shopOrders.getComment());
        }
        if (!StringUtils.isEmpty(shopOrders.getGmtCreate())) {
            criteria.andEqualTo("gmtCreate", shopOrders.getGmtCreate());
        }
        if (!StringUtils.isEmpty(shopOrders.getGmtModified())) {
            criteria.andEqualTo("gmtModified", shopOrders.getGmtModified());
        }
        if (!StringUtils.isEmpty(shopOrders.getDel())) {
            criteria.andEqualTo("del", shopOrders.getDel());
        }
        if (!StringUtils.isEmpty(shopOrders.getRefund())) {
            criteria.andEqualTo("refund", shopOrders.getRefund());
        }
        criteria.andEqualTo("del", false);
        example.orderBy("id").desc();
        List<ShopOrders> shopOrderss = shopOrdersMapper.selectByExample(example);
        return shopOrderss;
    }

    @Override
    public ShopOrders queryShopOrdersById(Long id) {
        return shopOrdersMapper.selectByPrimaryKey(id);
    }


    @Override
    public void addShopOrders(ShopOrders shopOrders) {
        shopOrders.setGmtCreate(new Date());
        shopOrdersMapper.insertSelective(shopOrders);
    }

    @Override
    public void removeShopOrders(Long id) {
        ShopOrders shopOrders = new ShopOrders();
        shopOrders.setId(id);
        shopOrders.setDel(true);
        shopOrdersMapper.updateByPrimaryKeySelective(shopOrders);
    }

    @Override
    public void modifyShopOrders(ShopOrders shopOrders) {
        shopOrders.setGmtModified(new Date());
        shopOrdersMapper.updateByPrimaryKeySelective(shopOrders);
    }

    @Override
    public List<ShopOrders> findListByCondition(ShopOrders shopOrders) {
        return shopOrdersMapper.selectOrderList(shopOrders);
    }

    @Override
    public ShopOrders findShopOrdersById(Long id) {
        ShopOrders param = new ShopOrders();
        param.setId(id);
        ShopOrders orders = shopOrdersMapper.selectOrdersDetailById(param);
        Assert.notNull(orders, "订单不存在");
        return orders;
    }

    @Override
    public void ship(ShopOrders orders) {
        InputValidator.checkNull(orders.getId(), "订单id");

        ShopOrders queryResult = shopOrdersMapper.selectByPrimaryKey(orders.getId());
        if (queryResult == null) {
            throw new CustomException("订单不存在, 请确认后重试");
        }

        if (!queryResult.getOrderStatus().equals(ShopOrdersStatusEnum.DELIVERED.name())) {
            throw new CustomException("订单状态不正确");
        }

        shopOrdersMapper.updateByPrimaryKeySelective(ShopOrders.builder().id(orders.getId())
                .orderStatus(ShopOrdersStatusEnum.SEND.name()).shippingAt(new Date()).build());
    }

}
